package com.asia5b.a5mall.home.activity

import android.text.TextUtils
import androidx.fragment.app.Fragment
import androidx.lifecycle.Observer
import androidx.viewpager2.widget.ViewPager2
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.asia5b.a5bmall.resource.ActCons
import com.asia5b.a5bmall.same.dialog.UpdateDialog
import com.asia5b.a5mall.home.R
import com.asia5b.a5mall.home.databinding.ActivityMainBinding
import com.asia5b.a5mall.home.fragment.CategoryFragment
import com.asia5b.a5mall.home.fragment.HomeFragment
import com.asia5b.a5mall.middleware.base.SimpleActivity
import com.asia5b.a5mall.middleware.config.*
import com.asia5b.a5mall.middleware.event.DownloadApkEvent
import com.asia5b.a5mall.middleware.event.LoginEvent
import com.asia5b.a5mall.middleware.event.LogoutEvent
import com.asia5b.a5mall.middleware.logic.viewmodel.welcome.AboutVM
import com.asia5b.a5mall.middleware.base.SimpleFragmentAdapter
import com.asia5b.a5mall.middleware.utils.*
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

/**
 * Created by zhangyuncai on 2019/12/21.
 * @Note   首页Activity
 */
@Route(path = ActCons.MainActivity)
class MainActivity : SimpleActivity<AboutVM, ActivityMainBinding>() {

    private var clickTime: Long = 0

    override val viewModelClazz: Class<AboutVM>
        get() = AboutVM::class.java

    override fun getDataBinding(): ActivityMainBinding {
        return ActivityMainBinding.inflate(layoutInflater)
    }


    override fun initView() {
        CountrysRepository.update()
        //如果没有路由就使用空白页面
        val cartFragment = (ARouter.getInstance().build(ActCons.CartFragment)
                .navigation()
                ?: ARouter.getInstance().build(ActCons.BlankFragment).navigation()) as Fragment
        val mineFragment = (ARouter.getInstance().build(ActCons.MineFragment)
                .navigation()
                ?: ARouter.getInstance().build(ActCons.BlankFragment).navigation()) as Fragment
        val fragmentList = arrayListOf(
                HomeFragment(),
                CategoryFragment(),
                cartFragment,
                mineFragment
        )
        val fragmentAdapter = SimpleFragmentAdapter(supportFragmentManager, lifecycle, fragmentList)
        mDataBinding.vpMain.adapter = fragmentAdapter
        mDataBinding.vpMain.offscreenPageLimit = fragmentList.size
        mDataBinding.vpMain.isUserInputEnabled = false
    }


    override fun initEvent() {
        UserRepository.updateInboxCount()

        mDataBinding.vpMain.registerOnPageChangeCallback(object : ViewPager2.OnPageChangeCallback() {
            override fun onPageSelected(p0: Int) {
                setAndroidNativeLightStatusBar((p0 == 1) || (p0 == 2))
            }
        })
        mDataBinding.tabMainBottom.itemIconTintList = null
        mDataBinding.tabMainBottom.setOnNavigationItemSelectedListener {
            when (it.itemId) {
                R.id.menu_cate -> {
                    mDataBinding.vpMain.setCurrentItem(1, false)
                }
                R.id.menu_cart -> {
                    mDataBinding.vpMain.setCurrentItem(2, false)
                }
                R.id.menu_mine -> {
                    mDataBinding.vpMain.setCurrentItem(3, false)
                }
                else -> {
                    mDataBinding.vpMain.setCurrentItem(0, false)
                }
            }
            return@setOnNavigationItemSelectedListener true
        }
    }

    override fun startObserver() {
        mViewModel.updateModelLiveData.observe(this, Observer { updateModel ->
            UpdateDialog.update(mActivity, updateModel)
        })
    }

    override fun initData() {
        checkUpdate()
    }

    fun checkUpdate() {
        mViewModel.checkUpdate()

    }


    override fun onBackPressed() {
        if (System.currentTimeMillis() - clickTime < 1000) {
            //按返回键返回桌面
            moveTaskToBack(true)
        } else {
            clickTime = System.currentTimeMillis()
            showMessage(getString(R.string.再按一次退出A5BMall))
        }
    }

    override fun onResume() {
        super.onResume()
    }


    override val isRegisterEventBus: Boolean
        get() = true

    @Subscribe(threadMode = ThreadMode.MAIN)
    fun onLoginEvent(event: LoginEvent) {
        UserRepository.updateInboxCount()
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    fun onLogoutEvent(event: LogoutEvent) {
        UserRepository.updateInboxCount()
        Asia5bPayUtils.AuthorizationValue = ""
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public fun onDownloadApkEvent(event: DownloadApkEvent) {
        if (!TextUtils.isEmpty(event.downloadApkFilePath)) {
            UpdateIntentService.installApk(mActivity, event.downloadApkFilePath!!)
        }
    }


}